package Texas;

import java.util.*;

import Texas.Card.Card;
import Texas.Exception.*;


public class Player {
	private String _name;
	private int _chips = 0;
	private int _dedication = 0;
	private boolean _raise = false, _allIn = false, _fold = false;
	private ArrayList<Card> _cardSet = new ArrayList<Card>();
	
	public Player(String name) {
		_name = name;
	}
	
	public String getName() {
		return _name;
	}	
	
	public void setName(String name) {
		_name = name;
	}
	
	public int getChips() {
		return _chips;
	}

	public void setChips(int chips) throws IllegalArgumentException {
		if(chips > 0)
			_chips = chips;
		else
			throw new IllegalArgumentException("chips > 0");
	}
	
	public void addChips(int chips) throws IllegalArgumentException {
		if(chips > 0)
			_chips += chips;
		else
			throw new IllegalArgumentException("chips > 0");
	}

	
	public void addCard(Card card) {
		if(_cardSet.size() < 2)
			_cardSet.add(card); 
	}

	public Card getCard(int i) {
		if(i >= 0 && i < _cardSet.size())
			return _cardSet.get(i);
		else
			return null;
	}
	
	public ArrayList<Card> getCards() {
		return _cardSet;
	}

	public int sizeOfCardSet() {
		return _cardSet.size();
	}
		
	public void clearCardSet() {
		_cardSet.clear();
	}
	
	public int getDediction() {
		return _dedication;
	}
	
	public void clearDediction() {
		_dedication = 0;
	}	
		
	public void payBlinds(int blindAmount) {
		if(blindAmount >= _chips) {
			try {
				allIn(blindAmount);
			} catch (Exception excep) {
				System.err.println(excep);
			}
		}
		else {
			try {
				call(blindAmount);
			} catch (Exception excep) {
				System.err.println(excep);
			}
		}
	}
		
	public void call(int minDediction) throws CallException{
		int n[] = new int[5];
		n[0]++;
		n[1]++;

		if(minDediction > _dedication && minDediction < _chips) {
			_chips -= (minDediction - _dedication);
			n[2]++;
			_dedication += (minDediction - _dedication);
			n[3]++;
		} else {
			throw new CallException("\nWBT Call\nn[0] = "+n[0]+"\nn[1] = "+n[1]+"\nn[4] = 1");
		}
		n[4]++;
		
		System.out.println("WBT Call");
		for(int i = 0; i < n.length; i++)
			System.out.println("n["+i+"] = " + n[i]);
	}
		
	public void raise(int dediction, int minDediction) throws RaiseException {
		int n[] = new int[6];
		n[0]++;
		n[1]++;
		
		if(dediction > minDediction && dediction < _chips) {
			_chips -= (dediction - _dedication);
			n[2]++;
			_dedication += (dediction - _dedication);
			n[3]++;
			_raise = true;
			n[4]++;
		} else {
			if(dediction >= _chips) {
				throw new RaiseException("\nWBT Raise\nn[0] = "+n[0]+"\nn[1] = "+n[1]+"\nn[5] = 1");
			} else if(minDediction >= dediction) {
				throw new RaiseException("\nWBT Raise\nn[0] = "+n[0]+"\nn[1] = "+n[1]+"\nn[5] = 1");
			}
		}
		n[5]++;
		
		System.out.println("WBT Raise");
		for(int i = 0; i < n.length; i++)
			System.out.println("n["+i+"] = " + n[i]);
	}

	public void allIn(int minDediction) throws AllInException {
		int n[] = new int[8];
		n[0]++;
		n[1]++;
		if(!_allIn && _chips != 0) {
			_dedication += _chips;
			n[2]++;
			_chips = 0;
			n[3]++;
			n[4]++;
			if(_dedication > minDediction) {
				_raise = true;
				n[5]++;
			}
			_allIn = true;
			n[6]++;
		} else {
			throw new AllInException("\nWBT AllIn\nn[0] = "+n[0]+"\nn[1] = "+n[1]+"\nn[7] = 1");			
		}
		n[7]++;
		
		System.out.println("WBT AllIn");
		for(int i = 0; i < n.length; i++)
			System.out.println("n["+i+"] = " + n[i]);
	}
	
	public void fold() {
		_fold = true;
	}
	
	public boolean hasFolded() {
		return _fold;
	}
	
	public boolean hasRaised() {
		return _raise;
	}
	
	public boolean isAllIn() {
		return _allIn;
	}
	
	public void clearStates() {
		_allIn = false;
		_raise = false;
		_fold = false;
	}
	
	public void establishTable(int playersCount) {
		Table table = new Table(this, playersCount);
		table.start();
	}
}
